package GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client.BusinessImplements;

import Controler.Delete_Subject_controler;

import Controler.Update_Subject_Controler;
import javax.swing.border.LineBorder;
/**
  * @author Lucie
 * Class name Delete_Subject_Panel
 * extend  Abstract_Jpanel
 * purpose: this panel get name of subject from the user and call the controller 
 * to delete this subject
 */
public class Delete_Subject_Panel extends Abstract_Jpanel  {

	private JComboBox comboBox; 
	private String[] all_subject_name;
	private String[] all_subject_id;
	private ArrayList<String> all_subject;
	/**
	 * Create the panel.
	 */
	public Delete_Subject_Panel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(47, 54, 560, 243);
		add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(57, 25, 447, 184);
		panel.add(panel_1);
		 panel_1.setLayout(null);
		
		 comboBox = new JComboBox();
		 comboBox.setBounds(206, 59, 136, 20);
		 panel_1.add(comboBox);
		 
		 JLabel lblSubjectName = new JLabel("subject name:");
		 lblSubjectName.setBounds(72, 59, 88, 17);
		 panel_1.add(lblSubjectName);
		 lblSubjectName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		 
		 JLabel lblChooseTheSubject = new JLabel("choose the subject you like to delete");
		 lblChooseTheSubject.setForeground(Color.RED);
		 lblChooseTheSubject.setFont(new Font("Segoe Print", Font.PLAIN, 13));
		 lblChooseTheSubject.setBounds(46, 99, 338, 20);
		 panel_1.add(lblChooseTheSubject);
		
		JLabel lblChoosesubjectTo = new JLabel("choose subject to delete");
		lblChoosesubjectTo.setForeground(new Color(25, 25, 112));
		lblChoosesubjectTo.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblChoosesubjectTo.setBounds(63, 6, 428, 37);
		add(lblChoosesubjectTo);
		/*
		 * btnBack ;When you click this button you will return to 
		 *	sing in panel screen
		 */
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();

				
			}
		});
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(104, 325, 95, 37);
		add(btnBack);
		/*
		 *btnGo=When you click this button you will call delete_subject( get_autor_id()))
		 * a method of the controller of this panel if this method return true we will
		 * call the methods of this class next_screen_success(),empty_text_fields();
		 * else we call next_screen_faild(delete_subject_controler.get_msg());	
		 * delete_subject_controler.get_msg(); return the msg from the controller with all
		 * the error msg.
		 */
		JButton btnGo = new JButton("delete");
		btnGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				if (!get_autor_id().equals(""))
				{
					Delete_Subject_controler delete_subject_controler=new Delete_Subject_controler();
					
					if(delete_subject_controler.delete_subject( get_autor_id()))
					{
						next_screen_success();
						empty_text_fields();
						
					}
					else {
						next_screen_faild(delete_subject_controler.get_msg());
						
					}
		
					
					
				}
				else
					error_msg();
				
				
			}
		});
		btnGo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnGo.setBounds(512, 325, 95, 37);
		add(btnGo);

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	/**
	 *  * this method is the first method we call in this screen
	 * the main purpose of this method is to initialize the comboBox 
	 * that this panel use, here we call the controller of this panel
	 * to get all subjects names and id's and set them at the combobox
	 */
	public void on_screen_load() {

		
		Update_Subject_Controler update_subject_controler=new  Update_Subject_Controler();
		all_subject=update_subject_controler.get_all_subject();
		
		String[] line;
		all_subject_name = new String[all_subject.size()+1];
		all_subject_id = new String[all_subject.size()+1];
		all_subject_name[0] = "";
		all_subject_id[0] =  "";
		for (int i = 1 ; i <= all_subject.size() ; i++)
		{
			line = all_subject.get(i-1).split(",");
			all_subject_id[i] = line[0];
			all_subject_name[i] = line[1];
			
		}
		
		comboBox.setModel(new DefaultComboBoxModel(all_subject_name));
		comboBox.setBounds(132, 32, 121, 20);
		
		
	}

	
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 
	 * @return subject id of the subject that was chosen in the combobox
	 */
	public String get_autor_id()
	{
		
		if (!comboBox.getSelectedItem().equals(""))
			{
				for(int i=0;i< all_subject_name.length;i++)
					if( all_subject_name[i].equals(comboBox.getSelectedItem()))
						
						return all_subject_id[i];
				   	
			
			}
		
		return "";
	}
	/**
	 *  this method display showMessageDialog with a failed msg
	 */
	public void error_msg()
	{
	JOptionPane.showMessageDialog(this,"miss  get_all_subject() deatails ");
	}
	/**
	 * this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"delete success");
	}
	/**
	 *   this method display showMessageDialog with a failed msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"delete faild "+msg);
	}
	/**
	 * this method initially the panel
	 */
	public void empty_text_fields()
	{
		on_screen_load();
		
	}
}

